package com.huike.task.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbRulePool;
import com.huike.clues.mapper.TbRulePoolMapper;
import com.huike.common.constant.ErrorMessageConstants;
import com.huike.common.constant.MagicConstants;
import com.huike.common.exception.BaseException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class RemindAndRecoveryTask {

    // cron表达式
    private static final String CRON = "0 0 8 * * ?"; // 每天上午8点

    @PostConstruct
    public void init() {
        updateRule();
        log.info("开启定时任务");
    }

    private final TaskUtil taskUtil;

    private final TbRulePoolMapper tbRulePoolMapper;

    // 线索回收
    private static Integer clueRecoveryTime;

    // 线索提醒
    private static Integer clueRemindTime;

    // 商机回收
    private static Integer businessRecoveryTime;

    // 商机提醒
    private static Integer businessRemindTime;

    /**
     * 定时任务
     */
    @Scheduled(cron = CRON)
    public void task() {
        // 线索回收
        List<TbAssignRecord> clueRecoveryList = taskUtil.getClueListByTime(clueRecoveryTime);
        List<Long> clueRecoveryAssignIds = new ArrayList<>(clueRecoveryList.size());
        List<Long> clueRecoveryClueIds = new ArrayList<>(clueRecoveryList.size());
        clueRecoveryList.forEach(i -> {
            clueRecoveryAssignIds.add(i.getId());
            clueRecoveryClueIds.add(i.getAssignId());
        });
        taskUtil.updateAssign(clueRecoveryAssignIds);
        taskUtil.updateClue(clueRecoveryClueIds);

        // 线索提醒
        List<TbAssignRecord> clueRemindList = taskUtil.getClueListByTime(clueRemindTime);
        List<Long> clueRemindClueIds = new ArrayList<>(clueRecoveryList.size());
        clueRecoveryList.forEach(i -> {
            clueRemindClueIds.add(i.getAssignId());
        });
        taskUtil.clueRemind(clueRemindClueIds);

        // 商机回收
        List<TbAssignRecord> businessRecoveryList = taskUtil.getBusinessListByTime(businessRecoveryTime);
        List<Long> businessRecoveryAssignIds = new ArrayList<>(clueRecoveryList.size());
        List<Long> businessRecoveryClueIds = new ArrayList<>(clueRecoveryList.size());
        clueRecoveryList.forEach(i -> {
            businessRecoveryAssignIds.add(i.getId());
            businessRecoveryClueIds.add(i.getAssignId());
        });
        taskUtil.updateAssign(businessRecoveryAssignIds);
        taskUtil.updateBusiness(businessRecoveryClueIds);

        // 商机提醒
        List<TbAssignRecord> businessRemindList = taskUtil.getBusinessListByTime(businessRemindTime);
        List<Long> businessRemindClueIds = new ArrayList<>(clueRecoveryList.size());
        clueRecoveryList.forEach(i -> {
            businessRemindClueIds.add(i.getAssignId());
        });
        taskUtil.businessRemind(businessRemindClueIds);
    }

    /**
     * 修改规则池规则时间，每次更新规则池需要调用此方法
     */
    public void updateRule() {
        LambdaQueryWrapper<TbRulePool> qClueRule = new LambdaQueryWrapper<>();
        qClueRule.eq(TbRulePool::getType, MagicConstants.CLUE);
        TbRulePool clueRule = tbRulePoolMapper.selectOne(qClueRule);
        // 线索回收时间
        clueRecoveryTime = getTimeByTbRulePool(clueRule.getLimitTimeType(), clueRule.getLimitTime());
        // 线索提醒时间
        clueRemindTime = getTimeByTbRulePool(clueRule.getWarnTimeType(), clueRule.getWarnTime());

        LambdaQueryWrapper<TbRulePool> qBusinessRule = new LambdaQueryWrapper<>();
        qBusinessRule.eq(TbRulePool::getType, MagicConstants.BUSINESS);
        TbRulePool businessRule = tbRulePoolMapper.selectOne(qBusinessRule);
        // 商机回收时间
        businessRecoveryTime = getTimeByTbRulePool(businessRule.getLimitTimeType(), businessRule.getLimitTime());
        // 商机提醒时间
        businessRemindTime = getTimeByTbRulePool(businessRule.getWarnTimeType(), businessRule.getWarnTime());
    }

    /**
     * 根据规则池获取时间
     *
     * @param type 提醒或回收
     * @param time 时间
     * @return 返回提醒或回收时间
     */
    private Integer getTimeByTbRulePool(String type, Integer time) {
        switch (type) {
            case "0":
                return time;
            case "1":
                return time * 24;
            case "2":
                return time * 24 * 7;
        }
        throw new BaseException(ErrorMessageConstants.RULE_POOL_EXCEPTION);
    }

}